<!-- 错题本 -->
<template>
  <div class="app-container">
    <!-- 搜索工具栏 -->
    <div class="toolbar">
      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item label="科目">
          <el-input
            v-model="queryParams.subject"
            placeholder="科目"
            clearable
          />
        </el-form-item>
        <el-form-item label="类型">
          <el-select
            v-model="queryParams.type"
            placeholder="题目类型"
            clearable
          >
            <el-option label="单选题" value="single" />
            <el-option label="多选题" value="multiple" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <div class="toolbar-buttons">
        <el-button type="primary" @click="handleGenerateExercise">
          <el-icon><Document /></el-icon>
          生成练习
        </el-button>
      </div>
    </div>

    <!-- 错题列表 -->
    <el-table v-loading="loading" :data="questionList" style="width: 100%">
      <el-table-column prop="questionType" label="类型" width="100">
        <template #default="{ row }">
          <el-tag :type="row.questionType === 'single' ? 'primary' : 'success'">
            {{ row.questionType === "single" ? "单选题" : "多选题" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="subject" label="科目" width="120" />
      <el-table-column
        prop="questionText"
        label="题目内容"
        show-overflow-tooltip
      />
      <el-table-column prop="options" label="选项" show-overflow-tooltip />
      <el-table-column prop="correctAnswer" label="正确答案" width="100" />
      <el-table-column prop="difficulty" label="难度" width="100">
        <template #default="{ row }">
          <el-tag :type="getDifficultyType(row.difficulty)">
            {{ row.difficulty }}
          </el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="wrongCount" label="错误次数" width="100" /> -->
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 生成练习对话框 -->
    <el-dialog
      title="生成练习"
      v-model="dialogVisible"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-form
        ref="exerciseFormRef"
        :model="exerciseForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="练习名称" prop="name">
          <el-input v-model="exerciseForm.name" placeholder="请输入练习名称" />
        </el-form-item>
        <el-form-item label="题目数量" prop="count">
          <el-input-number v-model="exerciseForm.count" :min="1" :max="50" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { getWrongQuestions, generateExercise } from "@/api/exam";

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  subject: "",
  type: "",
});

// 错题列表数据
const loading = ref(false);
const questionList = ref([]);
const total = ref(0);

// 获取错题列表
const getQuestionList = async () => {
  loading.value = true;
  try {
    const data = await getWrongQuestions(queryParams);
    questionList.value = data;
    total.value = data.total;
  } catch (error) {
    console.error("获取错题列表失败：", error);
  } finally {
    loading.value = false;
  }
};

// 查询按钮点击
const handleQuery = () => {
  queryParams.pageNum = 1;
  getQuestionList();
};

// 重置按钮点击
const resetQuery = () => {
  queryParams.subject = "";
  queryParams.type = "";
  handleQuery();
};

// 分页大小改变
const handleSizeChange = (val) => {
  queryParams.pageSize = val;
  getQuestionList();
};

// 页码改变
const handleCurrentChange = (val) => {
  queryParams.pageNum = val;
  getQuestionList();
};

// 获取难度标签类型
const getDifficultyType = (difficulty) => {
  switch (difficulty) {
    case "easy":
      return "success";
    case "medium":
      return "warning";
    case "hard":
      return "danger";
    default:
      return "info";
  }
};

// 生成练习相关
const dialogVisible = ref(false);
const exerciseFormRef = ref(null);
const exerciseForm = reactive({
  name: "",
  count: 10,
});

// 表单校验规则
const rules = {
  name: [{ required: true, message: "请输入练习名称", trigger: "blur" }],
  count: [{ required: true, message: "请输入题目数量", trigger: "blur" }],
};

// 生成练习按钮点击
const handleGenerateExercise = () => {
  exerciseForm.name = `错题练习-${new Date().toLocaleDateString()}`;
  dialogVisible.value = true;
};

// 提交表单
const handleSubmit = async () => {
  if (!exerciseFormRef.value) return;
  await exerciseFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        await generateExercise({
          questionCount: exerciseForm.count,
          paperName: exerciseForm.name,
        });
        ElMessage.success("生成练习成功");
        dialogVisible.value = false;
      } catch (error) {
        console.error("生成练习失败：", error);
      }
    }
  });
};

onMounted(() => {
  getQuestionList();
});
</script>

<style scoped>
.toolbar {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.search-form {
  flex: 1;
}

.toolbar-buttons {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>
